Test Series - Data Structure

Test Number 21/115

Q: To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?
A. 1
B. 2
C. 3
D. 4
Solution: Either the push or the pop has to be a costly operation, and the costlier operation requires two queues.
Q: Making the push operation costly, select the code snippet which implements the same.(let q1 and q2 be two queues)
A. public void push(int x) { if(empty()) { q1.offer(x); } else{ if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q2.offer(q1.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int s
B. public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q1.offer(x); int size = q1.size(); while(size>0) { q2.offer(q1.poll()); size--; } } else if(q2.size()>0) { q2.offer(x); int size = q2.size();
C. public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q1.offer(q2.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int size = q2.size();
D. public void push(int x) { if(empty()) { q1.offer(x); } else { if(q1.size()>0) { q2.offer(x); int size = q1.size(); while(size>0) { q2.offer(q2.poll()); size--; } } else if(q2.size()>0) { q1.offer(x); int size = q2.size();
Solution: Stack follows LIFO principle, hence a new item added must be the first one to exit, but queue follows FIFO principle, so when a new item is entered into the queue, it will be at the rear end of the queue. If the queue is initially empty, then just add the new element, otherwise add the new element to the second queue and dequeue all the elements from the second queue and enqueue it to the first one, in this way, the new element added will be always in front of the queue. Since two queues are needed to realize this push operation, it is considered to be costlier.
Q: Making the push operation costly, select the code snippet which implements the pop operation.
A. public void pop() { if(q1.size()>0) { q2.poll(); } else if(q2.size()>0) { q1.poll(); } }
B. public void pop() { if(q1.size()>0) { q1.poll(); } else if(q2.size()>0) { q2.poll(); } }
C. public void pop() { q1.poll(); q2.poll(); }
D. public void pop() { if(q2.size()>0) { q1.poll(); } else { q2.poll(); } }
Solution: As the push operation is costly, it is evident that the required item is in the front of the queue, so just dequeue the element from the queue.
Q: Select the code snippet which returns the top of the stack.
A. public int top() { if(q1.size()>0) { return q1.poll(); } else if(q2.size()>0) { return q2.poll(); } return 0; }
B. public int top() { if(q1.size()==0) { return q1.peek(); } else if(q2.size()==0) { return q2.peek(); } return 0; }
C. public int top() { if(q1.size()>0) { return q1.peek(); } else if(q2.size()>0) { return q2.peek(); } return 0; }
D. public int top() { if(q1.size()>0) { return q2.peek(); } else if(q2.size()>0) { return q1.peek(); } return 0; }
Solution: Assuming its a push costly implementation, the top of the stack will be in the front end of the queue, note that peek() just returns the front element, while poll() removes the front element from the queue.
Q: Select the code snippet which return true if the stack is empty, false otherwise.
A. public boolean empty() { return q2.isEmpty(); }
B. public boolean empty() { return q1.isEmpty() || q2.isEmpty(); }
C. public boolean empty() { return q1.isEmpty(); }
D. public boolean empty() { return q1.isEmpty() & q2.isEmpty(); }
Solution: If both the queues are empty, then the stack also is empty.
Q:  Making the pop operation costly, select the code snippet which implements the same.
A. public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>0) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q2.size(); while(count>0) q1.offer(q2.poll()); res = q2.poll(); } return res; }
B. public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>1) q2.offer(q1.poll()); res = q2.poll(); } if(q2.size()>0) { count = q2.size(); while(count>1) q1.offer(q2.poll()); res = q1.poll(); } return res; }
C. public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q1.size(); while(count>1) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q2.size(); while(count>1) q1.offer(q2.poll()); res = q2.poll(); } return res; }
D. public int pop() { int res=-999,count=0; if(q1.size()>0) { count = q2.size(); while(count>1) q2.offer(q1.poll()); res = q1.poll(); } if(q2.size()>0) { count = q1.size(); while(count>1) q1.offer(q2.poll()); res = q2.poll(); } return res; }
Solution: Here the pop operation is costly, hence we need two queues, other than the first element, all the the elements from one queue are dequeued and enqueued to the second queue, hence only one element remains in the first queue which is the item we want, so dequeue it and return the result.
Q: What is the functionality of the following piece of code?
public void fun(int x)
{
	q1.offer(x);
}
A. Perform push() with push as the costlier operation
B. Perform push() with pop as the costlier operation
C. Perform pop() with push as the costlier operation
D. Perform pop() with pop as the costlier operation
Solution: offer() suggests that it is a push operation, but we see that it is performed with only one queue, hence the pop operation is costlier.
Q: 
A. 
B. 
C. 
D. 
Solution: 

You Have Score    /8